from xml.dom.minidom import *
import os
from objects import System

class Map( object ):
    def __init__( self, cn, config):
        self.resource_path = config.resources.path
        print 'creating map from %s bytes' % len( cn )
        self.dom = parseString(''.join(cn))
        self.name = self.dom.getElementsByTagName('name')[0].firstChild.nodeValue
        self.systems = {}
        self.triggers = {}
        self.cache()
        self.dump()
        
    def dump(self):
        print 'Dumping map "%s"' % self.name
        for system_name in self.systems:
            system = self.systems[system_name]
            self.active_system = system
            print 'System: %s [width: %s, height: %s]' % (system_name, system.width, system.height)
            print 'Star location: %s, %s' % (system.star.x, system.star.y)
            for planet_name in system.planets:
                print '\tPlanet: %s' % planet_name
                planet = system.planets[planet_name]
                print '\t Type: %s' % planet.type
                print '\t Size: %s' % planet.size
                print '\t Location: %s, %s' % (planet.x, planet.y)
                        
    def cache(self):
        for n in self.dom.getElementsByTagName('system'):
            name = n.getAttribute('name')
            system = System(self, name, n)
            self.systems[name] = system
